﻿using Common;
using IBLL;
using Microsoft.AspNetCore.Mvc;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Unmanned_MSUI.Areas.APP.Filters;

namespace Unmanned_MSUI.Areas.APP.Controllers
{
    [Area("APP")]
    [UserFilter]
    public class UserInfoController : Controller
    {
        private IUserInfoBLL _userInfoBLL;
        public UserInfoController(
            IUserInfoBLL userInfoBLL
            )
        {
            _userInfoBLL = userInfoBLL;
        }

        //用户信息的显示
        public IActionResult ListView()
        {
            return View();
        }
        public IActionResult UserBalanceView()
        {
            return View();
        }

        //添加用户
        public IActionResult CreateUserInfoView()
        {
            return View();
        }
        //用户修改
        public IActionResult UpdateUserInfoView()
        {
            return View();
        }
        //修改资料
        public IActionResult UserSettingView()
        {
            return View();
        }
        public IActionResult UpdateUserPwdView()
        {
            return View();
        }

        #region 用户列表获取方法（GetUserInfos）

        [HttpGet]
        public IActionResult GetUserInfos(int page, int limit, string account, string userName)
        {
            int count;
            //调用BLL的获取用户方法
            List<GetUserInfosDTO> list = _userInfoBLL.GetUserInfos(page, limit, account, userName, out count);

            //返回结果
            ReturnResult result = new ReturnResult()
            {
                Code = 0,
                Msg = "获取成功!",
                Data = list,
                IsSuccess = true,
                Count = count
            };
            //返回结果
            return Json(result);
        }
        #endregion

        #region 添加用户的方法(CreateUserInfo)

        [HttpPost]
        public IActionResult CreateUserInfo([FromForm] UserInfo user)
        {
            string msg;
            //调用添加方法
            bool isSuccess = _userInfoBLL.CreateUserInfo(user, out msg);
            //返回结果封装
            ReturnResult result = new ReturnResult();
            result.Msg = msg;
            result.IsSuccess = isSuccess;

            //判断是否成功
            if (isSuccess)
            {
                result.Code = 200;
            }
            return Json(result);
        }

        #endregion

        #region 用户软删除的接口（DeleteUserInfo）

        [HttpPost]
        public IActionResult DeleteUserInfo(string id)
        {
            //实例化返回结果
            ReturnResult result = new ReturnResult();

            //非空判断
            if (string.IsNullOrWhiteSpace(id))
            {
                result.Msg = "id不能为空！";
                return Json(result);
            }
            //调用删除方法
            bool isOk = _userInfoBLL.DeleteUserInfo(id);
            //判断删除情况
            if (isOk)
            {
                result.Msg = "删除用户成功!";
                result.Code = 200;
            }
            //返回结果
            return Json(result);
        }


        #endregion

        #region 批量删除用户(DeleteUserInfo）

        [HttpPost]
        public IActionResult DeleteUserInfos(List<string> ids)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //非空判断
            if (ids == null || ids.Count == 0)
            {
                result.Msg = "你还没有选择要删除的用户";
                return Json(result);
            }
            //调用批量删除的方法
            bool isOk = _userInfoBLL.DeleteUserInfo(ids);
            if (isOk)
            {
                result.Msg = "删除成功！";
                result.Code = 200;
            }
            else
            {
                result.Msg = "删除失败！";
            }
            return Json(result);//返回结果
        }

        #endregion

        #region 修改用户的接口（UpdateUserInfo）

        [HttpPost]
        public IActionResult UpdateUserInfo([FromBody] UserInfo user)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //调用方法
            string msg;
            bool isOk = _userInfoBLL.UpdateUserInfo(user, out msg);
            result.Msg = msg;
            if (isOk)
            {
                result.Code = 200;
                result.IsSuccess = isOk;
            }
            return Json(result);

        }

        #endregion

        #region 根据用户id获取用户信息的接口(GetUserInfoById)

        /// <summary>
        /// 根据用户id获取用户信息的接口
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUserInfoById(string id)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            if (string.IsNullOrWhiteSpace(id))
            {
                result.Msg = "用户id不能为空!";
                return Json(result);
            }
            //调用方法获取用户信息
            var userInfo = _userInfoBLL.GetUserInfoById(id);

            result.Msg = "获取成功!";
            result.Code = 200;
            result.IsSuccess = true;
            result.Data = new
            {
                userInfo,
            };
            //返回结果
            return Json(result);
        }

        #endregion

        #region 更新用户信息接口(UpdateUserPwd)

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="id">用户id</param>
        /// <param name="oldPwd">旧密码</param>
        /// <param name="newPwd">新密码</param>
        /// <param name="againPwd">确认密码</param>
        /// <param name="msg">返回的消息</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateUserPwd(string id, string oldPwd, string newPwd, string againPwd)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            string msg;//声明返回的消息
            result.IsSuccess = _userInfoBLL.UpdateUserPwd(id, oldPwd, newPwd, againPwd, out msg);//调用更新密码方法
            result.Msg = msg;//赋值消息给返回对象
            //判断是否修改成功
            if (result.IsSuccess)
            {
                result.Code = 200;
            }
            //返回结果
            return Json(result);
        }

        #endregion

        #region 用户列表获取方法（GetUsers）

        [HttpGet]
        public IActionResult GetUsers(string account, string userName)
        {
            int count;
            //调用BLL的获取用户方法
            List<GetUserInfosDTO> list = _userInfoBLL.GetUsers(account, userName, out count);

            //返回结果
            ReturnResult result = new ReturnResult()
            {
                Code = 0,
                Msg = "获取成功!",
                Data = list,
                IsSuccess = true,
                Count = count
            };
            //返回结果
            return Json(result);
        }
        #endregion
    }
}
